package services

import (
	"gin-app/app/common/request"
	"gin-app/app/models"
	"gin-app/global"
)

type novelService struct {
}

var NovelService = new(novelService)

func (n novelService) CreateNovel(params request.CreateNovel, createUserId string) (err error, novel *models.Novel) {
	novel = models.NewNovel(params.Name, params.Author, params.Cover, params.Description, createUserId)
	err = global.App.DB.Create(novel).Error
	return
}
func (n novelService) GetNovelList() (err error, novel []models.Novel) {
	err = global.App.DB.Find(&novel).Error
	return
}

func (n novelService) GetNovel(params request.GetBook) (err error, books []models.Book) {
	err = global.App.DB.Where("book_id = ?", params.BookId).First(&books).Error
	return

}

// 现在只能通过书名查询书籍
func (n novelService) QueryNovel(params request.QueryNovel) (err error, books []models.Book) {
	//db := global.App.DB
	//db.Table("(?) as u", db.Model(&models.User{}).Select("name")).Select("id").Where("name LIKE  ?", params.Keyword).First(&models.User{})
	err = global.App.DB.Where("name  LIKE ?", "%"+params.Keyword+"%").Find(&books).Error
	return

}
